#pragma once
#include "GPInclude.h"
#include <windows.h>
#include <direct.h>
#include <Dbghelp.h>
using namespace std;
#pragma auto_inline (off)
#pragma comment(lib, "DbgHelp")

namespace TestDump
{
	LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo)
	{
		_mkdir("./ExceptionDump");
		HANDLE lhDumpFile = CreateFile(_T("./ExceptionDump/DumpFile.dmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL ,NULL);
		MINIDUMP_EXCEPTION_INFORMATION loExceptionInfo;
		loExceptionInfo.ExceptionPointers = ExceptionInfo;
		loExceptionInfo.ThreadId = GetCurrentThreadId();
		loExceptionInfo.ClientPointers = TRUE;
		MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),lhDumpFile, MiniDumpNormal, &loExceptionInfo, NULL, NULL);
		CloseHandle(lhDumpFile);
		return EXCEPTION_EXECUTE_HANDLER;
	}

	void ErrorFun()
	{
		char acName[256] = {0};
		std::string kString = "ErrorString";
		sprintf_s(acName,sizeof(acName),"%d ErrorSprint %s %d",15,kString,50);

		//char* pcError = NULL;
		//*pcError = 21;

		int j = 0;
		int i = 120 % j;
	}

	void TestFun()
	{
		__try
		{
			static BOOL s_b = FALSE;

			if(!s_b)
			{
				s_b = TRUE;
				ErrorFun();
			}
			else
			{
				MessageBox(NULL, _T("No Error"), _T(""), MB_OK);
			}
		}
		__except(MyUnhandledExceptionFilter(GetExceptionInformation()))
		{
			MessageBox(NULL, _T("Exception Fire!"), _T(""), MB_OK);
		}
	}

	void Test()
	{
		//SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
		TestFun();
		//TestFun();
	}
}





